import 'file:///D:/AndroidStudioProjects/flutter/ccshop/lib/ui/widget/home/HomeGridView.dart';
import 'package:ccshop/ui/widget/common/SearchBarView.dart';
import 'package:ccshop/ui/widget/home/HomeBottomListView.dart';
import 'package:ccshop/ui/widget/home/StickyTabBarDelegate.dart';
import 'file:///D:/AndroidStudioProjects/flutter/ccshop/lib/ui/widget/home/SwiperView.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  RefreshController _controller = RefreshController(initialRefresh: true);

  List<Tab> _list = [
    Tab(text: "猜你喜欢"),
    Tab(text: "限量特价"),
    Tab(text: "蔬菜豆制品"),
    Tab(text: "新鲜水果"),
    Tab(text: "肉禽蛋"),
    Tab(text: "海鲜水产"),
    Tab(text: "熟食凉菜"),
    Tab(text: "速冻食品"),
  ];

  void _onRefresh() async {
    await Future.delayed(Duration(seconds: 2));
    _controller.refreshCompleted();
  }

  void _onLoading() async {
    await Future.delayed(Duration(seconds: 2));
    _controller.loadComplete();
  }

  @override
  Widget build(BuildContext context) {
    var tabBar = TabBar(
      tabs: _list,
      labelStyle: TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
      labelColor: Colors.red,
      isScrollable: true,
      indicatorColor: Colors.red,
      indicatorSize: TabBarIndicatorSize.label,
      unselectedLabelColor: Colors.black,
      controller: DefaultTabController.of(context),
    );

    return Scaffold(
      appBar: AppBar(
        title: Text("超超商城"),
        centerTitle: true,
      ),
      body: SmartRefresher(
        enablePullDown: true,
        enablePullUp: true,
        header: WaterDropHeader(),
        footer: ClassicFooter(),
        controller: _controller,
        onRefresh: _onRefresh,
        onLoading: _onLoading,
        child: CustomScrollView(physics: ScrollPhysics(), slivers: <Widget>[
          SliverPersistentHeader(
            delegate: StickyTabBarDelegate(
              minHeight: 440.0,
              maxHeight: 440.0,
              child: Column(
                children: [
                  SearchBarView(),

                  // 轮播图
                  Container(
                    height: 200,
                    margin: EdgeInsets.all(10),
                    child: SwiperView(),
                  ),

                  // 菜单
                  Container(
                    height: 140,
                    margin: EdgeInsets.all(10),
                    child: HomeMenuGridView(),
                  ),
                ],
              ),
            ),
          ),
          SliverPersistentHeader(
            pinned: true,
            delegate: StickyTabBarDelegate(
              minHeight: 50.0,
              maxHeight: 50.0,
              child: Container(
                color: Color(0xFFFAFAFA),
                child: DefaultTabController(
                  // tabbar必须要用DefaultTabController包裹，否则会报错
                  length: _list.length,
                  child: tabBar,
                ),
              ),
            ),
          ),
          HomeBottomListView(),
        ]),
      ),
    );
  }
}
